package com.panghl.config;

import com.atomikos.jdbc.AtomikosDataSourceBean;
import com.mysql.cj.jdbc.MysqlXADataSource;
import org.apache.ibatis.session.SqlSessionFactory;
import org.mybatis.spring.SqlSessionFactoryBean;
import org.mybatis.spring.SqlSessionTemplate;
import org.mybatis.spring.annotation.MapperScan;
import org.springframework.beans.factory.annotation.Qualifier;
import org.springframework.boot.context.properties.ConfigurationProperties;
import org.springframework.boot.jdbc.DataSourceBuilder;
import org.springframework.context.annotation.Bean;
import org.springframework.context.annotation.Configuration;
import org.springframework.context.annotation.Primary;
import org.springframework.jdbc.datasource.DataSourceTransactionManager;

import javax.sql.DataSource;
import java.sql.SQLException;

/**
 * @Author panghl
 * @Date 2022/2/17
 * @Description TODO
 **/
@Configuration
@MapperScan(basePackages = "com.panghl.test01", sqlSessionFactoryRef = "test01SqlSessionFactory")
public class DataSource1Config {

    /**
     * 配置test01数据库
     *
     * @return
     */
    @Bean(name = "test01DataSource")
    public DataSource test01DataSource(DBConfig1 testConfig) throws SQLException {
        MysqlXADataSource mysqlXADataSource = new MysqlXADataSource();
        mysqlXADataSource.setUrl(testConfig.getUrl());
        mysqlXADataSource.setUser(testConfig.getUsername());
        mysqlXADataSource.setPassword(testConfig.getPassword());
        mysqlXADataSource.setPinGlobalTxToPhysicalConnection(true);

        // 创建Atomikos全局事务
        AtomikosDataSourceBean xaDataSource = new AtomikosDataSourceBean();
        xaDataSource.setXaDataSource(mysqlXADataSource);
        xaDataSource.setUniqueResourceName("test01DataSource");

        xaDataSource.setMinPoolSize(testConfig.getMinPoolSize());
        xaDataSource.setMaxPoolSize(testConfig.getMinPoolSize());
        xaDataSource.setMaxIdleTime(testConfig.getMaxIdleTime());
        xaDataSource.setMaxLifetime(testConfig.getMaxLifetime());
        xaDataSource.setLoginTimeout(testConfig.getLoginTimeout());
        xaDataSource.setBorrowConnectionTimeout(testConfig.getBorrowConnectionTimeout());
        xaDataSource.setTestQuery(testConfig.getTestQuery());
        return xaDataSource;
    }

    /**
     * test01 sql会话工厂
     */
    @Bean(name = "test01SqlSessionFactory")
    public SqlSessionFactory test01SqlSessionFactory(@Qualifier("test01DataSource") DataSource test01DataSource) throws Exception {
        SqlSessionFactoryBean bean = new SqlSessionFactoryBean();
        bean.setDataSource(test01DataSource);
        //bean.setMapperLocations(
        //        new PathMatchingResourcePatternResolver().getResource("classpath:mapping/test/*Mapper.xml")
        //);
        return bean.getObject();
    }


    @Bean(name = "test01SqlSessionTemplate")
    public SqlSessionTemplate test01SqlSessionTemplate(@Qualifier("test01SqlSessionFactory") SqlSessionFactory test01SqlSessionFactory) {
        return new SqlSessionTemplate(test01SqlSessionFactory);
    }

}
